import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import executor.Entity.AttributesVO;
import executor.Entity.Email;
import executor.Entity.Id;
import executor.Entity.JsonDataVO;
import executor.XxlJobExecutorApplication;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = XxlJobExecutorApplication.class)
public class WordTest2 {
    private static Logger log = Logger.getLogger(WordTest2.class);
    private static final String ENCODING = "UTF-8";
    private static Configuration cfg = new Configuration();

    //初始化cfg
    static {
        //设置模板所在文件夹
        cfg.setClassForTemplateLoading(WordTest2.class,"/demo/word");
        // setEncoding这个方法一定要设置国家及其编码，不然在ftl中的中文生成html后会乱码
        cfg.setEncoding(Locale.getDefault(),ENCODING);
        //设置对象包装器
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        // 设置异常处理器，这样的话就可以${a}没有报错
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
    }
    //获取模板对象
    public static Template getTemplate(String templateFileName) throws IOException {
        return cfg.getTemplate(templateFileName,ENCODING);
    }
    /**
     * 数据及模板生成文件
     * @param data Map的数据结果集
     * @param templateFileName ftl模板文件名
     * @param outFilePath 生成文件名称
     */

    public static File crateFile(Map<String,Object>data,String templateFileName , String outFilePath){
        Writer out = null;
        File outFile = new File(outFilePath);
        try {
            //获取模板，并设置编码方式，这个编码必须要与界面中的编码格式一致
            Template template = getTemplate(templateFileName);
            if (!outFile.getParentFile().exists()){
                outFile.getParentFile().mkdirs();
            }
            out = new OutputStreamWriter(new FileOutputStream(outFile),ENCODING);
            //处理模板
            template.process(data,out);
            out.flush();
            log.info("由模板文件" + templateFileName + "生成" + outFilePath + "成功.");
        }catch (Exception e){
            log.error("由模板文件" + templateFileName + "生成" + outFilePath + "出错.");
            e.printStackTrace();
        }finally {
            try {
                if (out !=null){
                    out.close();
                }
            }catch (IOException e){
                log.error("关闭Write对象出错",e);
                e.printStackTrace();
            }
        }

        return outFile;
    }

    public static void main(String[] args){
        try {
            String list="{\"data\":{\"id\":\"b74bdf7ac11499935ac3fa4e019fb98d\",\"type\":\"users\",\"attributes\":{\"nickname\":\"huyuan\",\"email\":\"huyuan@tiduyun.cn\",\"gavatar\":\"https://avatar-alioss.tower.im/letter_avatars/b74bdf7ac11499935ac3fa4e019fb98d_huyuan\",\"created_at\":\"2020-05-08T10:30:34.000+08:00\",\"updated_at\":\"2020-06-03T09:57:55.000+08:00\"}},\"jsonapi\":{\"version\":\"1.0\"}}";
//        List<Object> dataList = Arrays.asList(JSON.parseArray(list).toArray());
            JSONObject dataList = JSON.parseObject(list);
            Object data = dataList.get("data");
            System.out.println(data);
            JSONObject jsonObject = (JSONObject) JSON.parseObject(String.valueOf(data));

            Object attributes = jsonObject.get("attributes");
            System.out.println(attributes);

            JsonDataVO jsonDataVO = JSON.parseObject(String.valueOf(data), JsonDataVO.class);
            System.out.println(jsonDataVO);

            AttributesVO attributesVO = JSON.parseObject(String.valueOf(attributes), AttributesVO.class);
            System.out.println(attributesVO);

            Id id = JSON.parseObject(String.valueOf(data),Id.class);
            System.out.println(id);

            Email email = JSON.parseObject(String.valueOf(attributes),Email.class);
            System.out.println(email);
//        List<AttributesVO> AttributesVO = JSONObject.parseArray(dataList,AttributesVO.class);


//        JSONObject dataList = new JSONObject(list);
//                List<String> dataList = JSON.parseArray(list).toJavaList(String.class);

            System.out.println(dataList);
            Map<String,Object> data2 = new HashMap<String, Object>();
            data2.put("test1",jsonDataVO);
            data2.put("test2",attributesVO);
            data2.put("test3",id);
            data2.put("test4",email);
            data2.put("test5","测试5");
            data2.put("test6","测试6");
            data2.put("test7","测试7");
            List<Map<String,String>> educations = new ArrayList<Map<String, String>>();
            crateFile(data2,"demo2.ftl","C:/Users/12561/Desktop/demoTest.doc");
            System.out.println("添加成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
